<template>
  <view>
    <view class="search-bar">
    <!--  @blur="blur" @focus="focus" @cancel="cancel"-->
      <uni-search-bar radius="100" :focus="true" cancelButton="none" @input="input">
      </uni-search-bar>
    </view>
    
    <!-- 商品信息 -->
    <view class="search-container" v-if="searchList.length !== 0">
      <view class="search-goods" v-for="item in searchList" :key="item.goods_id" @click="gotoGoods(item.goods_id)">
        <view class="goods-title">{{item.goods_name}}</view>
        <uni-icons type="forward" size="16"></uni-icons>
      </view>
    </view>
  
    <!-- 历史查询记录 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="25" @click="clear"></uni-icons>
      </view>
      <view class="history-body">
        <uni-tag :text="item" v-for="(item, index) in historyList" :key="index" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
    
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        keywords: '',
        searchList: [],
        historyList: []
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods: {
      // 输入表单事件
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          if(e == '') {
            this.searchList = []
            return
          }
          this.keywords = e
          let item = this.historyList.indexOf(e)
          if(item == -1) {
            this.historyList.unshift(this.keywords)
          }
          this.saveSearchHistory()
          this.getSearchList()
        },1000)
      },
      
      // 获取搜索数据
      async getSearchList() {
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.keywords})
        if(res.meta.status !== 200) return uni.showMsg()
        this.searchList = res.message
      },
      
      // 跳转商品详情页
      gotoGoods(id) {
        uni.navigateTo({
          url: '/subpkg/goods-detail/goods-detail?id=' + id
        })
      },
      
      // 保存查询历史
      saveSearchHistory() {
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
    
      // 清除历史记录
      clear() {
        this.historyList = []
        uni.removeStorageSync('kw')
      },
      
      // 点击历史查询记录跳转页面
      gotoGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods-list/goods-list?query=' + item
        })
      }
    }
  }
</script>

<style lang="scss"> 
.search-bar {
  position: sticky;
  top: 0;
  z-index: 666;
}
.search-container {
  padding: 0 10rpx;
  .search-goods {
    font-size: 24rpx;
    padding: 26rpx 0;
    border-bottom: 1rpx solid #efefef;
    display: flex;
    justify-content: space-between;
    .goods-title {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      margin-right: 6rpx;
    }
  }
}

.history-box {
  padding: 0 10rpx;
  .history-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 60rpx;
    border-bottom: 1px solid #999999;
  }
  .history-body {
    margin-top: 10rpx;
    display: flex;
    flex-wrap: wrap;
    .uni-tag {
      margin: 10rpx;
    }
  }
}
</style>
